package onlinecalsys.server.model;

import java.rmi.RemoteException;
import java.util.ArrayList;

import onlinecalsys.server.controller.ServerController;
import onlinecalsys.server.database.DatabaseLayer;
import onlinecalsys.server.database.MySQLDatabaseLayer;
import onlinecalsys.server.objects.Appointment;

public class CalsysModel implements ServerModel {

	private DatabaseLayer dblayer;
	private ServerController controller;

	
	public CalsysModel(ServerController controller) {
		dblayer = MySQLDatabaseLayer.getInstance();
		this.controller = controller;
	}

	@Override
	public boolean addAppointment(Appointment app) {

		boolean status = dblayer.storeAppointment(app.getUsername(), app
				.getShortDateRep(), app.getHour(), app.getAppointment());
		System.out.println("model returning " + status);
		return status;

	}

	public ArrayList<ArrayList<String>> getAppointments(String date, String username) {
			try {
				return dblayer.getAppointments(date, username);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}

	public boolean deleteAppointment(int appointmentId, String username) {
		return dblayer.deleteAppointment(appointmentId, username);
	}

	@Override
	public boolean usernameChecking(String username, String password) {
		return dblayer.usernameChecking(username, password);
	}

	@Override
	public boolean saveNote(String note,String username) {
		return dblayer.saveNote(note, username);
	}
	
	@Override
	public String getNote(String username) {
		
		return dblayer.getNote(username);
	}
	
	

	@Override
	public boolean shareAppointment(String username, int id,
			String shareWithUser) throws RemoteException {
		Boolean status = dblayer.shareAppointment(username, id, shareWithUser);
		if(status) {
			controller.notifyObservers(shareWithUser);
			return status;
		} else {
			return status;
		}
			
		
	}

	@Override
	public ArrayList<ArrayList<String>> getSharedAppointments(String username) {
		return dblayer.getSharedAppointments(username);
	}

	@Override
	public boolean createNewUser(String username, String password) {
		return dblayer.createUser(username, password);
	}

	
}
